package agend.client.gui.panels.appointment;

import jade.gui.GuiEvent;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.plaf.basic.BasicBorders.RadioButtonBorder;

import agend.client.gui.utils.HandlerEvent;
import agend.client.gui.utils.HandlerView;



/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
/**
 *
 * @author My
 */
public class AppointmentPanel extends javax.swing.JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private DxAppointmentPanel dxAppointmentPanel;
	private SxAppointmentPanel sxAppointmentPanel;
	private JButton setButton;
	/**
	 * Creates new form AppointmentPanel
	 */
	public AppointmentPanel() {
		initComponents();
		addListeners();
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */

	// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		dxAppointmentPanel = new DxAppointmentPanel();
		sxAppointmentPanel = new SxAppointmentPanel();

		setButton = new JButton();

		setMaximumSize(new java.awt.Dimension(394, 350));
		this.setPreferredSize(new java.awt.Dimension(799, 441));
		setMinimumSize(new java.awt.Dimension(394, 350));
		setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.Y_AXIS));
		JPanel panelUp = new JPanel();
		JPanel panelDown = new JPanel();

		panelUp.setLayout(new javax.swing.BoxLayout(panelUp, javax.swing.BoxLayout.X_AXIS));
		panelUp.add(sxAppointmentPanel);
		panelUp.add(dxAppointmentPanel);


		setButton.setText("SET EVENT");
		panelDown.add(setButton);


		add(panelUp);

		add(panelDown);




	}// </editor-fold>//GEN-END:initComponents

	private void addListeners()
	{
		setButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(sxAppointmentPanel.checkIfAEmptyField() && sxAppointmentPanel.checkIfDataGreaterThan() && sxAppointmentPanel.checkJSpinnerFasciaOraria())
				{
					
					if(sxAppointmentPanel.getButtonRadioPreferences().isSelected())
							sxAppointmentPanel.checkComboBoxPreferenceSelected();		
						
					//GuiEvent guiEvent = HandlerEvent.createRequestAppointmentEvent(sxAppointmentPanel.getTitle(), sxAppointmentPanel.getWhere(), sxAppointmentPanel.getDescription(), sxAppointmentPanel.getInvited(), dxAppointmentPanel.getTimeStart(), dxAppointmentPanel.getTimeEnd());
					if(checkDxModel() )
					{
						GuiEvent guiEvent = HandlerEvent.createRequestAppointmentEvent(sxAppointmentPanel.getModel(),dxAppointmentPanel.getModel());
						HandlerView.getAgent().postGuiEvent(guiEvent);
					}
				
					
					
				}
				
			}
		});
	}
	
	private boolean  checkDxModel(){
		
		int numeroContatti =0;
				
		if(dxAppointmentPanel.getModel() != null)
			numeroContatti = (Integer) dxAppointmentPanel.getModel().get("NumeroContatti");
		
		if(numeroContatti == 0){
			
			JOptionPane.showMessageDialog(AppointmentPanel.this,"Attenzione devi selezionare i contatti","Errore Selezione",JOptionPane.ERROR_MESSAGE);
			return false;
		}
		else if(numeroContatti == 1){
			
			return true;
		}
		else{
			int numeroMinimoInvitati = (Integer) dxAppointmentPanel.getModel().get("NumeroMinimoInvitati");
			int numeroMassimoInvitati = (Integer) dxAppointmentPanel.getModel().get("NumeroMassimoInvitati");
			
			if(numeroContatti>=numeroMinimoInvitati && numeroContatti>=numeroMassimoInvitati)
				return true;
			
			else{
				
			
				JOptionPane.showMessageDialog(AppointmentPanel.this,"Numero di contatti fuori dal range","Errore Selezione",JOptionPane.ERROR_MESSAGE);
				return false;
				
			
		}}
		
		
	}
	
	/**
	 * Effettuo 2  controlli su pannello di destra 
	 * 1- Se ho selezionato almeno un invitato
	 * 2- Se ho impostato un numero minimo e massiamo di invitati controllo che sia nel Rannge 
	 * @return true se tutto ok false altrimenti
	 */
	private boolean checkOnDxPAnel(){
		
		if(dxAppointmentPanel.getModel() == null){
			
			JOptionPane.showMessageDialog(AppointmentPanel.this,"Attenzione devi selezionare i contatti","Errore Selezione",JOptionPane.ERROR_MESSAGE);
			return false;
		}
		
		else if(!dxAppointmentPanel.getFieldMinimoNumeroInvitati().getText().equals("")  ){
			
			
			
			
		}
			
			
		return true;
	}
}
